package com.tianya.portal.crawler.controller;

import com.tianya.portal.blog.domain.BlogNewsBook;
import com.tianya.portal.blog.domain.BlogNewsRecall;
import com.tianya.portal.blog.domain.BlogNoteBook;
import com.tianya.portal.blog.service.IBlogNewsBookService;
import com.tianya.portal.blog.service.IBlogNewsRecallService;
import com.tianya.portal.blog.service.IBlogNoteBookService;
import com.tianya.portal.common.utils.MarkDownUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * 爬虫解析-针对爬虫访问进行优化
 */
@Controller
@RequestMapping
public class AnalysisController {
    private static final Logger logger = LoggerFactory.getLogger(AnalysisController.class);
    @Autowired
    private IBlogNewsRecallService blogNewsRecallService;

    @Autowired
    private IBlogNewsBookService blogNewsBookService;

    @Autowired
    private IBlogNoteBookService blogNoteBookService;

    private String prefix = "crawler/analysis";

    /**
     * 学习笔记详情
     */
    @GetMapping("/noteBookDetail")
    public String noteBookDetail(HttpServletRequest request, Long id, String password, ModelMap mmap) {
        BlogNoteBook blogNoteBook = blogNoteBookService.selectBlogNoteBookById(id, password);
        blogNoteBook.setContent(MarkDownUtils.getRendererHtml(blogNoteBook.getContent()));
        mmap.put("blogNoteBook", blogNoteBook);
        logger.info("爬虫访问：" + request.getHeader("user-agent"));
        return prefix + "/noteBook";
    }

    /**
     * 朝花夕拾详情
     */
    @GetMapping("/newsRecallDetail")
    public String newsRecallDetail(HttpServletRequest request, String id, ModelMap mmap) {
        BlogNewsRecall blogNewsRecall = blogNewsRecallService.selectBlogNewsRecallById(id);
        mmap.put("blogNewsRecall", blogNewsRecall);
        logger.info("爬虫访问：" + request.getHeader("user-agent"));
        return prefix + "/newsRecall";
    }

    /**
     * 日常笔记详情
     */
    @GetMapping("/newsBookDetail")
    public String newsBookDetail(HttpServletRequest request, String id, ModelMap mmap) {
        BlogNewsBook blogNewsBook = blogNewsBookService.selectBlogNewsBookById(id);
        mmap.put("blogNewsBook", blogNewsBook);
        logger.info("爬虫访问：" + request.getHeader("user-agent"));
        return prefix + "/newsBook";
    }
}
